import sys
import os
import time
import math
BASE_PATH = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)
from bin import get_Webdriver
from page_object.public import Public

class Analyzer_Manage:
    def __init__(self):
        self.driver = get_Webdriver.Get_Webdriver().driver

    def jumptoAnalyzerpage(self):
        curl = Public().getCurrentUrl()
        print(curl)
        if '/analysis/main.html' not in curl:
            self.driver.click("link_text=>数据分析工具")
            self.driver.click("link_text=>分析器")
        else:
            self.driver.F5()

    def clickUploadButton(self):
        """点击上传按钮"""
        self.driver.click("id=>ctlBtn")

    def inputAnalyzerName(self, name):
        """输入的插件名称"""
        self.driver.type("id=>fileName", name)

    def uploadFile(self, filePath):
        """上传分析器文件 """
        self.driver.type("css=>input[name='file']", filePath)
        time.sleep(0.5)

    def getPreviewFileName(self):
        """获得上传文件名称的预览"""
        if self.driver.get_text("css=>.info"):
            return self.driver.get_text("css=>.info")
        else:
            return "无内容正在显示"

    def deleteUploadFile(self):
        """删除准备上传的文件"""
        self.driver.click("css=>.webuploadDelbtn")

    def selectChartType(self,type="0"):
        """选择图表类型
        可选  0 地图    1 折线图   2 柱状图   3 饼图    4 词云图"""
        self.driver.click("css=>.checkbox-inline>input[value='{}']".format(type))

    def getAnalyzerCount(self):
        """获取当前页面下分析器个数"""
        time.sleep(1)
        try:
            lastcount = int(self.driver.get_text('css=>.pagination-info').split(" ")[3])
        except IndexError:
            return "文件列表为空"
        else:
            firstcount = int(self.driver.get_text('css=>.pagination-info').split(" ")[1])
            count = lastcount - firstcount
            return count

    def getCurrentAnalyzerList(self):
        """获取当前页面分析器列表"""
        resultCount=self.getAnalyzerCount()
        if resultCount == "分析器列表为空":
            return "分析器列表为空"
        else:
            fileList = []
            for i in range(0, resultCount):
                fileList.append(self.driver.get_text("css=>tr[data-index='{}']".format(i)))
            return fileList


if __name__ == '__main__':
    pass
